package jy.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jy.dao.DataDaoI;
import jy.model.Tdata;
import jy.pageModel.Data;
import jy.pageModel.DataGrid;
import jy.pageModel.PageHelper;
import jy.service.DataServiceI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DataServiceImpl implements DataServiceI {
	
	@Autowired
	private DataDaoI dataDao;

	@Override
	public List<Tdata> getDataList() {
		return dataDao.find("from Tdata t");
	}

	@Override
	public DataGrid dataGrid(Tdata data, PageHelper ph) {
		DataGrid dg = new DataGrid();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Tdata t ";
		List<Tdata> dl = dataDao.find(hql, params, ph.getPage(), ph.getRows());
		dg.setRows(dl);
		dg.setTotal(dataDao.count("select count(*) " + hql, params));
	
		return dg;
	}

	@Override
	public Tdata get(String dataname) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("dataname", dataname);
		Tdata t = dataDao.get("from Tdata t where t.dataname = :dataname", params);
		return t;
	}

	@Override
	public void edit(Data data) {
		try {
			Tdata t= this.get(data.getDataname());
			t.setDatavalue(data.getDatavalue());
			System.out.println(data.getDatavalue()+"--------------------");
			dataDao.save(t);

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}


}
